package com.payment.web.controller.app;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.payment.common.base.AjaxResult;
import com.payment.common.utils.StringUtils;
import com.payment.system.domain.Acq;
import com.payment.system.domain.PaymentOrder;
import com.payment.system.service.IAcqService;
import com.payment.system.service.IIndustryMccService;
import com.payment.system.service.IPaymentOrderService;

/**
 * @author fuzhili
 * @Description: app端 通道处理
 * @date 2019/1/28 9:07
 */
@Controller
@RequestMapping("/app/acq")
public class AppAcqController {
    private static final Logger logger = LoggerFactory.getLogger(AppAcqController.class);
    @Autowired
    private IAcqService acqService;
    @Autowired
    private IPaymentOrderService paymentOrderService;
    @Autowired
    private IIndustryMccService mccService;

    /**
     * app 端获取通道列表
     * @param type 通道支持的类型  wk[无卡],dh[养卡代还],yk[异卡完美],qe[全额]
     * @return
     */
    @RequestMapping("/getList")
    @ResponseBody
    public AjaxResult getList(String type,String bankCode) {
    	List<Acq> acqList=acqService.selectAcqListByApp(type);
    	List<Acq> romveList=new ArrayList<Acq>();
    	for (Acq acq : acqList) {
    		if(!StringUtils.isEmpty(acq.getSupportBankcode())){
    			String [] support = acq.getSupportBankcode().split(",");
    			if(support != null && !ArrayUtils.contains(support, bankCode)){
    				romveList.add(acq);
    			}
    		}
    		if(!StringUtils.isEmpty(acq.getNoSupportBankcode())){
    			String [] noSuppor = acq.getNoSupportBankcode().split(",");
    			if(noSuppor != null && ArrayUtils.contains(noSuppor, bankCode)){
    				romveList.add(acq);
    			}
    		}
		}
    	acqList.removeAll(romveList);
        return AjaxResult.successAndData("请求成功", acqList);
    }

	/**
	 * 查询用户最新的一条 无卡记录
	 *
	 * @return
	 */
	@RequestMapping("/getNewestOrderWK")
	@ResponseBody
	public AjaxResult getNewestOrderWKByMerchantId(String creditNumber) {
		return paymentOrderService.getNewestOrderWKByCreditCardIdId(creditNumber);
	}

    /**
     * 无卡收款 通道信息
     *
     * @param orderAmount
     * @param bankId
     * @param cvn
     * @param expdate
     * @param bankPhone
     * @param bankAccount
     * @param idCardNumber
     * @param bankAccountName
     * @return
     */
    @RequestMapping("/getAcq")
    @ResponseBody
    public AjaxResult getAcq(BigDecimal orderAmount, String bankId,
                             String bankAccountName, String idCardNumber, String bankAccount
            , String bankPhone, String expdate, String cvn) {
        return acqService.getAcq(orderAmount, bankId,
                bankAccountName, idCardNumber, bankAccount
                , bankPhone, expdate, cvn);
    }
    
    /**
     * 亿贤开通通道
     * @param merchantId 商户id
     * @param companyId
     * @param bankAccountName 信用卡号
     * @param bankAccount 
     * @param idCardNumber
     * @param bankPhone
     * @param bankName
     * @param cvn
     * @param expdate
     * @param trxSourceIp
     * @return
     */
	@RequestMapping("/yxAcqBindCard")
	@ResponseBody
	public AjaxResult acqBindCard(String merchantId, String companyId, String bankAccountName, String bankAccount,
			String idCardNumber, String bankPhone, String bankName, String cvn, String expdate, String trxSourceIp) {
		return null;
	}
    
    /**
     * 获取行业信息
     *
     * @return
     */
    @RequestMapping("/getMccList")
    @ResponseBody
    public AjaxResult getMccList() {
        try {
            return mccService.getMccList();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取行业信息系统异常:" + e.toString());
            return AjaxResult.error("系统异常");
        }
    }


    /**
     * 光谷快捷消费接口
     */
    @RequestMapping("/gaungGuQuickPay")
    @ResponseBody
    public AjaxResult gaungGuQuickPay(@RequestParam Map<String, Object> map) {
        try {
            String pa = JSONObject.toJSONString(map);
            return acqService.GaungGuQuickPay(JSONObject.parseObject(pa));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("光谷消费接口系统异常:", e);
            return AjaxResult.error("消费系统繁忙，请联系客服查看问题");
        }
    }


    /**
     * app用户提现出款
     * @param merId  用户id
     * @param txnAmt  金额
     * @param trxSourceIp ip
     * @param payPwd  支付密码
     * @return
     */
    @RequestMapping("/paymentOut")
    @ResponseBody
    public AjaxResult paymentOut(String merId, BigDecimal txnAmt, 
    		String trxSourceIp,String payPwd) {
        return acqService.paymentOut(merId, txnAmt, trxSourceIp,payPwd);
    }

    /**
     * 光谷无卡出款
     */
    @RequestMapping("/guangGuOut")
    @ResponseBody
    public AjaxResult guangGuOut(String orderid) {
        PaymentOrder o = paymentOrderService.selectPaymentOrderById(orderid);
        return acqService.guangGuOut(o, o.getMerchantId());
    }

}
